/**
 *
 */
package com.leadership.api.web.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.demo2do.core.entity.exception.AuthorizationException;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;

/**
 * @author lute
 */
@RestControllerAdvice(annotations = RestController.class)
public class GlobalExceptionAdviceController {

    /**
     * @param exception
     * @return
     */
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    public HttpResult handleBusinessException(BusinessException exception) {
        HttpResult result = new HttpResult();
        result.fail(exception.getCode()).message(exception.getMessage());
        return result;
    }

    /**
     * @param exception
     * @return
     */
    @ResponseBody
    @ExceptionHandler(AuthorizationException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public HttpResult handleAuthorizationException(AuthorizationException exception) {
        HttpResult result = new HttpResult();
        result.fail(exception.getCode()).message(exception.getMessage());
        return result;
    }

}
